﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UniversityManagementSystem.Manager;
using iTextSharp.text;
using iTextSharp.text.pdf;
using UniversityManagementSystem.UI;

namespace UniversityManagementSystem.DAL
{
    [Serializable]
    public class Course
    {
        public string CourseTitle { get; set; }
        public string CourseName { get; set; }
        public double CourseCredit { get; set; }
        public string CourseDescription { get; set; }
        public string CourseDepartment { get; set; }
        public int CouresSemester { get; set; }
        public string CourseTeacher { get; set; }
        public string ObtainedGradeLetter { get; set; }

        CourseManager manager = new CourseManager();
        public delegate void UpdateColor(string message);
        public event UpdateColor OnUpdateColor;
        public void ColorChange(string message)
        {
            OnUpdateColor(message);
        }
        public string SaveCourse(string courseCredit)
        {
            try
            {
                if (!manager.IsNullOrBlank(courseCredit) && isNumber(courseCredit))
                {
                    CourseCredit = Convert.ToDouble(courseCredit);
                    return manager.SaveCourse(this);
                }
                else
                {
                    return "Please enter valid number as the credit of the course.";
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
            
        }
        public bool isNumber(string courseCredit)
        {
            return manager.isNumber(courseCredit);
        }

        public List<Course> GetCourse(string department)
        {
            return manager.GetCourse(department);
        }

        public string AssignCourse(Teacher aTeacher, Course aCourse)
        {
            return manager.AssignCourse(aTeacher,aCourse);
        }

        public List<Course> GelAllCourseBySemester(string department, string semester)
        {
            return manager.GelAllCourseBySemester(department, semester);
        }
        public List<Course> GelAllCourseBySemesterView(string department, string semester)
        {
            return manager.GelAllCourseBySemesterView(department, semester);
        }

        public List<Course> ShowUnassignedCourses(string department,string semester)
        {
            return manager.ShowUnassignedCourses(department, semester);
        }
        public PdfPTable CreatePdfUnassignedCourses(GridView unassignedCoursesDataGridView,string department)
        {
            int noOfColumns = 0, noOfRows = 0;
            DataTable tbl = null;

            if (unassignedCoursesDataGridView.AutoGenerateColumns)
            {
                tbl = unassignedCoursesDataGridView.DataSource as DataTable;
                noOfColumns = tbl.Columns.Count;
                noOfRows = tbl.Rows.Count;
            }
            else
            {
                noOfColumns = unassignedCoursesDataGridView.Columns.Count;
                noOfRows = unassignedCoursesDataGridView.Rows.Count;
            }

            float HeaderTextSize = 10;
            float ReportNameSize = 10;
            float ReportTextSize = 8;
            float ApplicationNameSize = 10;

            Document document = null;

            document = new Document(PageSize.A4, 0, 0, 15, 5);

            PdfPTable mainTable = new PdfPTable(noOfColumns);

            mainTable.HeaderRows = 4;

            PdfPTable headerTable = new PdfPTable(2);

            Phrase phApplicationName = new Phrase("Unassigned Courses", FontFactory.GetFont("Arial", ApplicationNameSize, Font.NORMAL));

            PdfPCell clApplicationName = new PdfPCell(phApplicationName);
            clApplicationName.Border = PdfPCell.BOX;
            clApplicationName.HorizontalAlignment = Element.ALIGN_LEFT;
            Phrase phDate = new Phrase("Generated at : " + DateTime.Now.ToString("dd/MM/yyyy"), FontFactory.GetFont("Arial", ApplicationNameSize, Font.NORMAL));

            PdfPCell clDate = new PdfPCell(phDate);
            clDate.HorizontalAlignment = Element.ALIGN_RIGHT;
            clDate.Border = PdfPCell.BOX;

            headerTable.AddCell(clApplicationName);
            headerTable.AddCell(clDate);
            headerTable.DefaultCell.Border = PdfPCell.NO_BORDER;

            PdfPCell cellHeader = new PdfPCell(headerTable);
            cellHeader.Border = PdfPCell.NO_BORDER;
            cellHeader.Colspan = noOfColumns;
            mainTable.AddCell(cellHeader);

            Phrase phHeader = new Phrase(department, FontFactory.GetFont("Arial", ReportNameSize, Font.BOLD));
            PdfPCell clHeader = new PdfPCell(phHeader);
            clHeader.Colspan = noOfColumns;
            clHeader.Border = PdfPCell.NO_BORDER;
            clHeader.HorizontalAlignment = Element.ALIGN_CENTER;
            mainTable.AddCell(clHeader);

            Phrase phSpace = new Phrase("\n");
            PdfPCell clSpace = new PdfPCell(phSpace);
            clSpace.Border = PdfPCell.NO_BORDER;
            clSpace.Colspan = noOfColumns;
            mainTable.AddCell(clSpace);



            for (int i = 0; i < noOfColumns; i++)
            {
                Phrase ph = null;

                ph = new Phrase(unassignedCoursesDataGridView.Columns[i].HeaderText, FontFactory.GetFont("Arial", HeaderTextSize, Font.BOLD));
                mainTable.AddCell(ph);
            }

            for (int rowNo = 0; rowNo < noOfRows; rowNo++)
            {
                for (int columnNo = 0; columnNo < noOfColumns; columnNo++)
                {

                    if (unassignedCoursesDataGridView.Columns[columnNo] is TemplateField)
                    {
                        DataBoundLiteralControl lc = unassignedCoursesDataGridView.Rows[rowNo].Cells[columnNo].Controls[0] as DataBoundLiteralControl;
                        string s = lc.Text.Trim();
                        Phrase ph = new Phrase(s, FontFactory.GetFont("Arial", ReportTextSize, Font.NORMAL));
                        mainTable.AddCell(ph);
                    }
                    else
                    {
                        string s = unassignedCoursesDataGridView.Rows[rowNo].Cells[columnNo].Text.Trim();
                        Phrase ph = new Phrase(s, FontFactory.GetFont("Arial", ReportTextSize, Font.NORMAL));
                        mainTable.AddCell(ph);
                    }
                }
            }
            mainTable.CompleteRow();
            return mainTable;
        }

        public List<Course> ShowUnschedeuledCourses(string department, string semester)
        {
            return manager.ShowUnschedeuledCourses(department, semester);
        }

        public PdfPTable CreatePdfUnscheduledCourses(GridView unscheduledCourseDataGridView, string department)
        {
            int noOfColumns = 0, noOfRows = 0;
            DataTable tbl = null;

            if (unscheduledCourseDataGridView.AutoGenerateColumns)
            {
                tbl = unscheduledCourseDataGridView.DataSource as DataTable;
                noOfColumns = tbl.Columns.Count;
                noOfRows = tbl.Rows.Count;
            }
            else
            {
                noOfColumns = unscheduledCourseDataGridView.Columns.Count;
                noOfRows = unscheduledCourseDataGridView.Rows.Count;
            }

            float HeaderTextSize = 10;
            float ReportNameSize = 10;
            float ReportTextSize = 8;
            float ApplicationNameSize = 10;

            Document document = null;

            document = new Document(PageSize.A4, 0, 0, 15, 5);

            PdfPTable mainTable = new PdfPTable(noOfColumns);

            mainTable.HeaderRows = 4;

            PdfPTable headerTable = new PdfPTable(2);

            Phrase phApplicationName = new Phrase("Unscheduled Courses", FontFactory.GetFont("Arial", ApplicationNameSize, Font.NORMAL));

            PdfPCell clApplicationName = new PdfPCell(phApplicationName);
            clApplicationName.Border = PdfPCell.BOX;
            clApplicationName.HorizontalAlignment = Element.ALIGN_LEFT;
            Phrase phDate = new Phrase("Generated at : " + DateTime.Now.ToString("dd/MM/yyyy"), FontFactory.GetFont("Arial", ApplicationNameSize, Font.NORMAL));

            PdfPCell clDate = new PdfPCell(phDate);
            clDate.HorizontalAlignment = Element.ALIGN_RIGHT;
            clDate.Border = PdfPCell.BOX;

            headerTable.AddCell(clApplicationName);
            headerTable.AddCell(clDate);
            headerTable.DefaultCell.Border = PdfPCell.NO_BORDER;

            PdfPCell cellHeader = new PdfPCell(headerTable);
            cellHeader.Border = PdfPCell.NO_BORDER;
            cellHeader.Colspan = noOfColumns;
            mainTable.AddCell(cellHeader);

            Phrase phHeader = new Phrase(department, FontFactory.GetFont("Arial", ReportNameSize, Font.BOLD));
            PdfPCell clHeader = new PdfPCell(phHeader);
            clHeader.Colspan = noOfColumns;
            clHeader.Border = PdfPCell.NO_BORDER;
            clHeader.HorizontalAlignment = Element.ALIGN_CENTER;
            mainTable.AddCell(clHeader);

            Phrase phSpace = new Phrase("\n");
            PdfPCell clSpace = new PdfPCell(phSpace);
            clSpace.Border = PdfPCell.NO_BORDER;
            clSpace.Colspan = noOfColumns;
            mainTable.AddCell(clSpace);



            for (int i = 0; i < noOfColumns; i++)
            {
                Phrase ph = null;

                ph = new Phrase(unscheduledCourseDataGridView.Columns[i].HeaderText, FontFactory.GetFont("Arial", HeaderTextSize, Font.BOLD));
                mainTable.AddCell(ph);
            }

            for (int rowNo = 0; rowNo < noOfRows; rowNo++)
            {
                for (int columnNo = 0; columnNo < noOfColumns; columnNo++)
                {

                    if (unscheduledCourseDataGridView.Columns[columnNo] is TemplateField)
                    {
                        DataBoundLiteralControl lc = unscheduledCourseDataGridView.Rows[rowNo].Cells[columnNo].Controls[0] as DataBoundLiteralControl;
                        string s = lc.Text.Trim();
                        Phrase ph = new Phrase(s, FontFactory.GetFont("Arial", ReportTextSize, Font.NORMAL));
                        mainTable.AddCell(ph);
                    }
                    else
                    {
                        string s = unscheduledCourseDataGridView.Rows[rowNo].Cells[columnNo].Text.Trim();
                        Phrase ph = new Phrase(s, FontFactory.GetFont("Arial", ReportTextSize, Font.NORMAL));
                        mainTable.AddCell(ph);
                    }
                }
            }
            mainTable.CompleteRow();
            return mainTable;
        }

        public List<Course> GetAllCourses()
        {
            return manager.GetAllCourses();
        }

        public List<Course> GetAllCoursesByDepartment(string department)
        {
            return manager.GetAllCoursesByBepartment(department);
        }

        public List<Course> GetAllEnrolledCourses(string registrationNo)
        {
            return manager.GetAllEnrolledCourses(registrationNo);
        }

        public bool CheckCourse(string registrationNo, string courseName)
        {
            return manager.CheckCourse(registrationNo, courseName);
        }

        public string SaveResult(string registrationNo, string courseName, string gradePoint)
        {
            return manager.SaveResult(registrationNo,courseName,gradePoint);
        }
    }
}